﻿using HomeWork.IBLL;
using HomeWork.MODEL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HomeWork.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ActionController : ControllerBase
    {
        IActionBLL bll;
        ILogger<ActionController> logger;
        public ActionController(IActionBLL bll, ILogger<ActionController> logger)
        {
            this.bll = bll;
            this.logger = logger;
        }
        /// <summary>
        /// 添加活动
        /// </summary>
        /// <param name="action">实体</param>
        /// <returns>受影响的行数</returns>
        [HttpPost]
        public async Task<int> AddAciton(ActionInfo action)
        {
            try
            {
                logger.LogInformation("添加成功");
                return await bll.AddAciton(action);
            }
            catch (Exception ex)
            {
                logger.LogError("添加失败");
                throw ex;
            }
        }

        /// <summary>
        /// 活动列表
        /// </summary>
        /// <param name="ActionName">活动名称</param>
        /// <param name="ActionType">活动类型</param>
        /// <returns>符合条件的数据</returns>
        [HttpGet]
        public IQueryable<ActionInfo> GetActionsList(string? ActionName = null, string? ActionType = null)
        {
            try
            {
                logger.LogInformation("获取活动列表");
                return bll.GetActionsList(ActionName, ActionType);
            }
            catch (Exception ex)
            {
                logger.LogError("获取列表失败");
                throw ex;
            }
        }

        /// <summary>
        /// 反填活动
        /// </summary>
        /// <param name="ActionId">活动编号</param>
        /// <returns>符合条件的数据</returns>
        [HttpGet]
        public async Task<ActionInfo> FindAction(int ActionId)
        {
            try
            {
                logger.LogInformation("反填成功");
                return await bll.FindAction(ActionId);
            }
            catch (Exception ex)
            {
                logger.LogError("反填失败");
                throw ex;
            }
        }
        /// <summary>
        /// 修改活动
        /// </summary>
        /// <param name="action">实体</param>
        /// <returns>受影响的行数</returns>
        [HttpPut]
        public async Task<int> UpdateAction(ActionInfo action)
        {
            try
            {
                logger.LogInformation("修改成功");
                return await bll.UpdateAction(action);
            }
            catch (Exception ex)
            {
                logger.LogError("修改失败");
                throw ex;
            }
        }
    }
}
